package algo.array;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/4/25 15:35
 * @Version 1.0
 */
public class GenericArray<E> {
    private E[] data;
    private int size;

    // 构造函数
    public GenericArray(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    // 无参默认构造
    public GenericArray() {
        this(10);
    }

    // 1扩容，变为new Capacity大小, O(n)
    private void resize(int capacity) {
        E[] newData = (E[]) new Object[capacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    // 1获取数组容量
    public int getCapacity() {
        return data.length;
    }

    // 1获取数组中元素个数
    public int getSize() {
        return size;
    }

    // 1返回数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 2获取index处元素
    public E get(int index) {
        checkIndex(index);
        return data[index];
    }

    // 2尾部获取
    public E getLast() {
        return get(size - 1);
    }

    // 2头部获取
    public E getFirst() {
        return get(0);
    }

    // 2查找数组是否有元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    // 2查找数组中元素e所在的索引，不存在返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    // 3在index处插入新元素e
    public void add(int index, E e) {
        checkIndexForAdd(index);
        // 如果当前元素个数等于数组容量，扩容2倍
        if (size == data.length) {
            resize(2 * data.length);
        }
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        ++size;
    }

    // 3末尾添加
    public void addLast(E e) {
        add(size, e);
    }

    // 3头部添加
    public void addFirst(E e) {
        add(0, e);
    }

    // 4修改index处值为新元素e
    public void set(int index, E e) {
        checkIndex(index);
        data[index] = e;
    }

    // 5删除index处元素，返回删除元素
    public E remove(int index) {
        checkIndex(index);
        E ret = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i] = data[i + 1];
        }
        --size;
        data[size] = null;

        // 缩容
        if (size == data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }
        return ret;
    }

    // 5删除头部,返回删除元素
    public E removeFirst() {
        return remove(0);
    }

    // 5删除末尾，返回删除元素
    public E removeLast() {
        return remove(size -1);
    }

    // 5删除元素e
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1) {
            remove(index);
        }
    }

    // checkIndexForAdd
    private void checkIndexForAdd(int index) {
        // when adding, because the array can resize, index could be equal to size.
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Failed. Index is illegal. Require index >=0 and index <= size.");
        }
    }

    private void checkIndex(int index) {
        // when finding or removing, because the array need be legal, index could not be equal to size.
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Failed. Index is illegal. Require index >=0 and index < size.");
        }
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array size = %d, capacity = %d \n", size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1) {
                res.append(',');
            }
        }
        res.append(']');
        return res.toString();
    }
}
